package com.dc.toolkit.hutool.core;

import cn.hutool.core.util.StrUtil;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class StrUtilTest {

    String STR_EMPTY = "";
    String STR_NULL = null;
    String STR_BLANK = " ";
    String STR_A = "a";
    String STR_1 = "1";
    String STR_DEF = "def";

    /**
     * 判断字符串是否为 null 或 ""
     */
    @Test
    public void testIsEmpty(){
        assertThat(StrUtil.isEmpty(STR_EMPTY), equalTo(true));
        assertThat(StrUtil.isEmpty(STR_NULL), equalTo(true));
        assertThat(StrUtil.isEmpty(STR_BLANK), equalTo(false));
    }

    /**
     * 判断字符串是否不为 null 或 ""
     */
    @Test
    public void testIsNotEmpty(){
        assertThat(StrUtil.isNotEmpty(STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isNotEmpty(STR_NULL), equalTo(false));
        assertThat(StrUtil.isNotEmpty(STR_BLANK), equalTo(true));
    }

    /**
     * 判断字符串是否为 null 或 "" 或 "  "
     */
    @Test
    public void testIsBlank(){
        assertThat(StrUtil.isBlank(STR_EMPTY), equalTo(true));
        assertThat(StrUtil.isBlank(STR_NULL), equalTo(true));
        assertThat(StrUtil.isBlank(STR_BLANK), equalTo(true));
    }

    /**
     * 判断字符串是否不为 null 或 "" 或 "  "
     */
    @Test
    public void testIsNotBlank(){
        assertThat(StrUtil.isNotBlank(STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isNotBlank(STR_NULL), equalTo(false));
        assertThat(StrUtil.isNotBlank(STR_BLANK), equalTo(false));
    }

    /**
     * 只要包含一个 null 或 "" 就为true
     */
    @Test
    public void testHasEmpty(){
        assertThat(StrUtil.hasEmpty(STR_A, STR_1), equalTo(false));
        assertThat(StrUtil.hasEmpty(STR_A, STR_1, STR_EMPTY), equalTo(true));
        assertThat(StrUtil.hasEmpty(STR_A, STR_1, STR_NULL), equalTo(true));
        assertThat(StrUtil.hasEmpty(STR_A, STR_1, STR_BLANK), equalTo(false));
    }

    /**
     * 只要包含一个 null 或 "" 或 "  ", 就为true
     */
    @Test
    public void testHasBlank(){
        assertThat(StrUtil.hasBlank(STR_A, STR_1), equalTo(false));
        assertThat(StrUtil.hasBlank(STR_A, STR_1, STR_EMPTY), equalTo(true));
        assertThat(StrUtil.hasBlank(STR_A, STR_1, STR_NULL), equalTo(true));
        assertThat(StrUtil.hasBlank(STR_A, STR_1, STR_BLANK), equalTo(true));
    }


    /**
     * 所有数据都为 null 或 ""时， 就为true
     */
    @Test
    public void testIsAllEmpty(){
        assertThat(StrUtil.isAllEmpty(STR_A, STR_1), equalTo(false));
        assertThat(StrUtil.isAllEmpty(STR_A, STR_1, STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isAllEmpty(STR_A, STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllEmpty(STR_EMPTY, STR_NULL), equalTo(true));
        assertThat(StrUtil.isAllEmpty(STR_EMPTY, STR_NULL, STR_BLANK), equalTo(false));
    }

    /**
     * 所有数据都为 null 或 "" 或 "   " 时， 就为true
     */
    @Test
    public void testIsAllBlank(){
        assertThat(StrUtil.isAllBlank(STR_A, STR_1), equalTo(false));
        assertThat(StrUtil.isAllBlank(STR_A, STR_1, STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isAllBlank(STR_A, STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllBlank(STR_EMPTY, STR_NULL), equalTo(true));
        assertThat(StrUtil.isAllBlank(STR_EMPTY, STR_NULL, STR_BLANK), equalTo(true));
    }

    /**
     * 所有数据都不为 null 或 ""时， 就为true
     */
    @Test
    public void testIsAllNotEmpty(){
        assertThat(StrUtil.isAllNotEmpty(STR_A, STR_1), equalTo(true));
        assertThat(StrUtil.isAllNotEmpty(STR_A, STR_1, STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isAllNotEmpty(STR_A, STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllNotEmpty(STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllNotEmpty(STR_EMPTY, STR_NULL, STR_BLANK), equalTo(false));
    }

    /**
     * 所有数据都不为 null 或 "" 或 "   "时， 就为true
     */
    @Test
    public void testIsAllNotBlank(){
        assertThat(StrUtil.isAllNotBlank(STR_A, STR_1), equalTo(true));
        assertThat(StrUtil.isAllNotBlank(STR_A, STR_1, STR_EMPTY), equalTo(false));
        assertThat(StrUtil.isAllNotBlank(STR_A, STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllNotBlank(STR_EMPTY, STR_NULL), equalTo(false));
        assertThat(StrUtil.isAllNotBlank(STR_EMPTY, STR_NULL, STR_BLANK), equalTo(false));
    }

    @Test
    public void testNullToEmpty(){
        assertThat(STR_EMPTY, equalTo(StrUtil.nullToEmpty(STR_NULL)));
    }

    @Test
    public void testEmptyToNull(){
        assertThat(STR_NULL, equalTo(StrUtil.emptyToNull(STR_EMPTY)));
    }

    /**
     * 当数据 仅 为 null 时，会被转为 默认值
     */
    @Test
    public void testNullToDefault(){
        assertThat(STR_DEF, equalTo(StrUtil.nullToDefault(STR_NULL, STR_DEF)));
        assertThat(STR_EMPTY, equalTo(StrUtil.nullToDefault(STR_EMPTY, STR_DEF)));
    }

    /**
     * 当数据 为 null 或 "" 时，会被转为 默认值
     */
    @Test
    public void testEmptyToDefault(){
        assertThat(STR_DEF, equalTo(StrUtil.emptyToDefault(STR_EMPTY, STR_DEF)));
        assertThat(STR_DEF, equalTo(StrUtil.emptyToDefault(STR_NULL, STR_DEF)));
    }

    /**
     * 当数据 为 null 或 "" 或 "  " 时，会被转为 默认值
     */
    @Test
    public void testBlankToDefault(){
        assertThat(STR_DEF, equalTo(StrUtil.blankToDefault(STR_EMPTY, STR_DEF)));
        assertThat(STR_DEF, equalTo(StrUtil.blankToDefault(STR_NULL, STR_DEF)));
        assertThat(STR_DEF, equalTo(StrUtil.blankToDefault(STR_BLANK, STR_DEF)));
    }

    /**
     * 测试 StrUtil.removePrefix()    去除前缀
     */
    @Test
    public void testRemovePrefix(){
        assertThat("abc", equalTo(StrUtil.removePrefix("pre_abc", "pre_")));
    }

    /**
     * 测试 StrUtil.removeSuffix()    去除字符串后缀
     */
    @Test
    public void testRemoveSuffix(){
        assertThat("abc", equalTo(StrUtil.removeSuffix("abc.jpg", ".jpg")));
    }
    
    /**
     * 测试 sub()
     */
    @Test
    public void testSub(){
        String str = "abcdefgh";
        assertThat("c", equalTo(StrUtil.sub(str, 2, 3)));
        assertThat("cde", equalTo(StrUtil.sub(str, 2, -3)));
        assertThat("c", equalTo(StrUtil.sub(str, 3, 2)));
    }
    
    /**
     * 获取 bytes()  字符串转 byte数组
     */
    @Test
    public void testBytes(){
        byte[] butes = StrUtil.bytes("今天是2021年4月24");
    }

    /**
     * 测试 str()
     */
    @Test
    public void testStr(){
        assertThat(STR_DEF, equalTo(StrUtil.str(STR_DEF)));
        assertThat(STR_NULL, equalTo(StrUtil.str(STR_NULL)));
    }
    
    /**
     * 测试 字符串模板替换
     */
    @Test
    public void testFormat(){
        String template = "{}爱{}，就像老鼠爱大米";
        System.out.println(StrUtil.format(template, "我"));
        System.out.println(StrUtil.format(template, "我", "你"));
        System.out.println(StrUtil.format(template, "我", "你", "他"));
        assertThat("我爱你，就像老鼠爱大米", equalTo(StrUtil.format(template, "我", "你")));
    }


    /**
     * 测试 StrUtil.count()    获取字符串中，另一个字符串出现的次数
     */
    @Test
    public void testCount(){
        assertThat(2, equalTo(StrUtil.count("abcaba123", "ab")));
    }

    /**
     * 测试 字符串替换
     */
    @Test
    public void testReplace(){
        String msg = "aBc123aBcd12345";
        assertThat("***123***d12345", equalTo(StrUtil.replace(msg, "aBc", "***")));

        assertThat("aBc123aBcd12345", equalTo(StrUtil.replace(msg, "abc", "***")));
        assertThat("***123***d12345", equalTo(StrUtil.replaceIgnoreCase(msg, "abc", "***")));
    }

    /**
     * 测试 字符串的长度
     */
    @Test
    public void testLength(){
        assertThat(3, equalTo(StrUtil.length("1bc")));
    }

}